// src/api/contractPaymentApi.js
import { apiClient } from './base'

// 分页查询付款记录列表
export const getList = (params = {}) => {
  const { page = 1, size = 20, search, contractId, paymentSide } = params
  const queryParams = new URLSearchParams()
  
  if (page) queryParams.append('page', page)
  if (size) queryParams.append('size', size)
  if (search) queryParams.append('search', search)
  if (contractId) queryParams.append('contractId', contractId)
  if (paymentSide) queryParams.append('paymentSide', paymentSide)
  
  return apiClient.get(`/contract-payments?${queryParams.toString()}`)
}

// 创建付款记录
export const create = (paymentData) => 
  apiClient.post('/contract-payments', paymentData)

// 根据ID查询付款记录详情
export const getById = (id) => 
  apiClient.get(`/contract-payments/${id}`)

// 更新付款记录
export const update = (id, paymentData) => 
  apiClient.put(`/contract-payments/${id}`, paymentData)

// 删除付款记录
export const deleteById = (id) => 
  apiClient.delete(`/contract-payments/${id}`)

// 检查付款编号是否存在
export const checkPaymentNumber = (paymentNumber, excludeId = null) => {
  const queryParams = new URLSearchParams()
  queryParams.append('paymentNumber', paymentNumber)
  if (excludeId) queryParams.append('excludeId', excludeId)
  
  return apiClient.get(`/contract-payments/check?${queryParams.toString()}`)
}

// 导出Excel
export const exportData = (params) => {
  const queryParams = new URLSearchParams()
  
  // 添加搜索参数
  if (params.search) {
    queryParams.append('search', params.search)
  }
  
  // 添加合同ID筛选
  if (params.contractId !== null && params.contractId !== undefined) {
    queryParams.append('contractId', params.contractId)
  }
  
  // 添加付款方筛选
  if (params.paymentSide !== null && params.paymentSide !== undefined) {
    queryParams.append('paymentSide', params.paymentSide)
  }
  
  const url = `/contract-payments/export${queryParams.toString() ? `?${queryParams.toString()}` : ''}`
  
  return apiClient.get(url, { 
    responseType: 'blob'
  })
}

// 默认导出
export default {
  getList,
  create,
  getById,
  update,
  delete: deleteById,
  checkPaymentNumber,
  export: exportData
} 